package com.argrace.smart.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.argrace.smart.R;
import com.argrace.smart.adapter.SubDeviceListAdapter;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.model.LocalDb_QingJing_DeviceTable;
import com.argrace.smart.ui.EmptyRecycleView;
import com.argrace.smart.util.Logg;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/2/11.
 * 情景模式详情界面
 */

public class QingJingDetailActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    EmptyRecycleView recyclerView;
    SwipeRefreshLayout refreshLayout;
    private SubDeviceListAdapter adapter;
    private ArrayList<LocalDb_QingJing_DeviceTable> lists = new ArrayList<>();
    private String uuid;
    private String name;

    private ArrayList<MineZigBeeDeviceListEntity> exchangesubDeviceLists(List<LocalDb_QingJing_DeviceTable> beans) {
        if (beans == null) {
            return new ArrayList<MineZigBeeDeviceListEntity>();
        }
        Logg.i("info", "QingJingDetailActivity.exchangesubDeviceLists()start,size = " + beans.size());
        ArrayList<MineZigBeeDeviceListEntity> exchange_lists = new ArrayList<>();
        for (int i = 0; i < beans.size(); i++) {
            MineZigBeeDeviceListEntity entity = new MineZigBeeDeviceListEntity();
            entity.setName(beans.get(i).getName());
            entity.setModel(beans.get(i).getModel());
            entity.setUuid(beans.get(i).getUuid());
            exchange_lists.add(entity);
        }
        return exchange_lists;

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qingjingdetail);
        init();
    }

    private void init() {
        uuid = getIntent().getStringExtra("uuid");
        name = getIntent().getStringExtra("name");
        setView();
        setTitle(true, false, "情景详情");
        showRight(true, R.mipmap.head_title_add);
        //进入添加情景里的设备列表
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(QingJingDetailActivity.this, CreateQingJing_ChooseSubDeviceActivity.class);
                intent.putExtra("uuid", uuid);
                intent.putExtra("name", name);
                startActivity(intent);
            }
        });
//        onRefresh();
    }

    @SuppressLint("ResourceType")
    private void setView() {
        recyclerView = (EmptyRecycleView) findViewById(R.id.recyclerView);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setItemAnimator(null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorPrimary));//getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme)
        refreshLayout.setOnRefreshListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        onRefresh();
    }

    private void getQingJingSubDeviceList() {
        Logg.i("info", "QingJingDetailActivity.getQingJingSubDeviceList()start,uuid=" + uuid);
        if (uuid == null) {
            toastActBtm("加载错误！");
            return;
        }
        List<LocalDb_QingJing_DeviceTable> beans = DataSupport.where("LocalDb_DeviceTable_deviceId = ?", name).find(LocalDb_QingJing_DeviceTable.class);
        if (beans != null) {

            adapter = new SubDeviceListAdapter(exchangesubDeviceLists(beans), this);
            adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            adapter.isFirstOnly(false);
            adapter.setlongListeners(new SubDeviceListAdapter.OnAdapterItemLongClickListener() {
                @Override
                public void click(MineZigBeeDeviceListEntity bean, int position) {
                    DataSupport.deleteAll(LocalDb_QingJing_DeviceTable.class, "uuid = ?", bean.getUuid());

                }
            });
            adapter.setListeners(new SubDeviceListAdapter.OnAdapterItemClickListener() {
                @Override
                public void click(MineZigBeeDeviceListEntity bean, int position) {
                    Intent intent;
                    if (bean.getModel().equals(ConstantConfigure.MODEL_SANWEIDUOKONG)
                            || bean.getModel().equals(ConstantConfigure.MODEL_LIANGWEIDUOKONG)
                            || bean.getModel().equals(ConstantConfigure.MODEL_YIWEIDUOKONG)
                            ) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_KaiguanActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    }
                    //场景开关
                    else if (bean.getModel().equals(ConstantConfigure.MODEL_SANWEIFUKONG)
                            || bean.getModel().equals(ConstantConfigure.MODEL_LIANGWEIFUKONG)
                            || bean.getModel().equals(ConstantConfigure.MODEL_YIWEIFUKONG)
                            || bean.getModel().equals(ConstantConfigure.MODEL_CHANGJINGKAIGUAN)
                            ) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_FuKongActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    }
                    //水浸控制界面
                    else if (bean.getModel().equals(ConstantConfigure.MODEL_SUIJINTANCEQI)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_ShuiJinActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    }
                    //烟雾、门磁、煤气（气体探测器）报警控制界面,红外感应（水地暖移除）
                    else if (bean.getModel().equals(ConstantConfigure.MODEL_YANWUTANCEQI) || bean.getModel().equals(ConstantConfigure.MODEL_MENCIGANYING) || bean.getModel().equals(ConstantConfigure.MODEL_MEIQITANCEQI) || bean.getModel().equals(ConstantConfigure.MODEL_RENTIGANYING)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_BaoJingActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    } else if (bean.getModel().equals(ConstantConfigure.MODEL_XINFENGKONGZHIQI)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_XinFengKongZhiQi.class);
                        intent.putExtra("deviceinfo", bean);
                    } else if (bean.getModel().equals(ConstantConfigure.MODEL_SHUIDINUAN) ||bean.getModel().equals(ConstantConfigure.MODEL_DIANDINUAN)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_ShuidinuanActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    } else if (bean.getModel().equals(ConstantConfigure.MODEL_PM25CHUANGANQI) || bean.getModel().equals(ConstantConfigure.MODEL_KONGQIHEZI)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_PM25Activity.class);
                        intent.putExtra("deviceinfo", bean);
                    } else if (bean.getModel().equals(ConstantConfigure.MODEL_10ACHAZUO)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_ChaZuoActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    } else if (bean.getModel().equals(ConstantConfigure.MODEL_ZHINENGMENSUO)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_MenSuoActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    } else if (bean.getModel().equals(ConstantConfigure.MODEL_KAIHELIANDIANJI)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_ChuangLianActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    }
                    else if (bean.getModel().equals(ConstantConfigure.MODEL_KONGTIAOZHUANHUANQI)) {
                        intent = new Intent(QingJingDetailActivity.this, DeviceControl_AirConditionActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    }
                    else {
                        intent = new Intent(QingJingDetailActivity.this, MyDeviceControlActivity.class);
                        intent.putExtra("deviceinfo", bean);
                    }
                    intent.putExtra("type", ConstantConfigure.TYPE_SETTING);
                    intent.putExtra("name",name);
                    startActivity(intent);

                }
            });
            recyclerView.setAdapter(adapter);
        }
        setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        setRefreshing(true);
        getQingJingSubDeviceList();
    }


    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }
}
